//
//  AppendNoonBlanksVC.m
//  PlanDemo
//
//  Created by 在成长开发 on 2025/9/8.
//

#import "AppendNoonBlanksVC.h"
#import "PlanCell.h"
#import "WeekPlanLayout.h"

#import "CellModel+AppendNoodBottomRow.h"

@interface AppendNoonBlanksVC ()<WeekPlanLayoutDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) NSMutableArray <CellModel *>* dataSource;

@end

@implementation AppendNoonBlanksVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"补充上下午最后一行的空白活动格";
        
    self.dataSource = [CellModel appendNoonBottomCell];

    [self.view addSubview:self.collectionView];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.collectionView.frame = CGRectMake(0, self.view.safeAreaInsets.top, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom);
}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CellModel * model = self.dataSource[indexPath.item];
    return CGSizeMake((kSingleColWidth + kItemSpace) * model.useCol - kItemSpace, (kSingleRowWidth + kLineSpace) * model.useRow - kLineSpace);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout atRowForItemAtIndexPath:(NSIndexPath *)indexPath {
    CellModel * model = self.dataSource[indexPath.item];
    return model.row;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout atColForItemAtIndexPath:(NSIndexPath *)indexPath {
    CellModel * model = self.dataSource[indexPath.item];
    return model.col;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return kLineSpace;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return kItemSpace;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.dataSource count];
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PlanCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlanCell" forIndexPath:indexPath];
    
    CellModel * model = self.dataSource[indexPath.item];
    
    cell.label.text = model.content;
    
    
    
    switch (model.type) {
        case CellType_Fixed: {
            cell.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
            cell.layer.borderWidth = 1;
            cell.layer.borderColor = [[UIColor purpleColor] colorWithAlphaComponent:0.3].CGColor;
        } break;
        case CellType_Text: {
            cell.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.1];
            cell.layer.borderWidth = 1;
            cell.layer.borderColor = [[UIColor cyanColor] colorWithAlphaComponent:0.3].CGColor;
        } break;
        default: {
            cell.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.1];
            cell.layer.borderWidth = 1;
            cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
        } break;
    }
    
    return cell;
}

#pragma mark - getter or setter
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        WeekPlanLayout * layout = [[WeekPlanLayout alloc] init];
        layout.delegate = self;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[PlanCell class] forCellWithReuseIdentifier:@"PlanCell"];
        
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _collectionView;
}

@end
